﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using TinyPMS_Jerusalem.Models.DBModel;

namespace TinyPMS_Jerusalem.Models.Validation
{
    #region CommentMustNotContainFilteredWords

    public class CommentMustNotContainFilteredWords : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            bool result = true;
            DBModel.DBModelDataContext db = new DBModel.DBModelDataContext();
            IEnumerable<DBModel.CommentFilter> query = from g in db.CommentFilter
                                                       select g;
            if (query.Count() > 0)
            {
                foreach (DBModel.CommentFilter filter in query)
                {
                    if (value.ToString().Contains(filter.FilterKeyword))
                    {
                        result = false;
                        break;
                    }
                }
            }
            return result;
        }
    }

    #endregion

    //#region BoolRequiredAttribute    

    ////Todo:做一个下拉框

    //public class BoolRequiredAttribute : ValidationAttribute
    //{
    //    public override bool IsValid(object value)
    //    {
    //        string val=value.ToString();
    //        if (val=="True"||val=="False")
    //            return true;
    //        else
    //            return false;
    //    }
    //}

    //#endregion

    #region CategoryMustExistAttribute

    public class CategoryMustExistAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null)
                return false;
            DBModelDataContext db = new DBModelDataContext();
            IEnumerable<PropertyCategory> query = from g in db.PropertyCategory
                                          where g.Name == value.ToString()
                                          select g;
            if (query.Count() > 0)
                return true;
            else
                return false;
        }
    }

    #endregion

    #region PropertyIDMustBeUnique
    
    public class PropertyIDMustBeUnique:ValidationAttribute
    {

        public override bool IsValid(object value)
        {
            DBModelDataContext db = new DBModelDataContext();
            IEnumerable<Property> query = from g in db.Property
                                          where g.ID == value.ToString()
                                          select g;
            if (query.Count() != 0)
                return false;
            else
                return true;
        }
    }
    
    #endregion
}